package com.sx.art2.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sx.art2.beans.Account;
import com.sx.art2.beans.Order;
import com.sx.art2.beans.ShangjiaAccount;
import com.sx.art2.mapper.AccountMapper;
import com.sx.art2.mapper.ArtMapper;
import com.sx.art2.mapper.OrderMapper;
import com.sx.art2.mapper.ShangjiaAccountMapper;
import com.sx.art2.service.OrderService;
@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	OrderMapper orderMapper;
	@Autowired
	ArtMapper artMapper;
	@Autowired
	AccountMapper accountMapper;
	@Autowired
	ShangjiaAccountMapper shangjiaAccountMapper;
	@Override
	@Transactional
	public void insert(Order order) {
		// TODO 自动生成的方法存根
		//1.生成订单
		//2.更新艺术品数量
		orderMapper.create(order);
		artMapper.updateArtSum(-1, order.getAuctionInfoId());
		
	}
	@Override
	@Transactional
	public void payment(String orderId) {
		// TODO 自动生成的方法存根
		SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//1.扣除费用
		Order order=orderMapper.selectByOrderId(orderId);
		Account account =new Account();
		account.setAccountTrade(order.getOrderPrice());
		account.setAccountBalance(accountMapper.selectBalanceByUserId(order.getUserId())-order.getOrderPrice());
		account.setAccountDate(dateFormat.format(new Date()));
		account.setAccountType("5");
		account.setUserId(order.getUserId());
		accountMapper.add(account);
		
		ShangjiaAccount shangjiaAccount=new ShangjiaAccount();
		shangjiaAccount.setAccountBalance(shangjiaAccountMapper.selectBalance()+order.getOrderPrice());
		shangjiaAccount.setAccountTrade(order.getOrderPrice());
		shangjiaAccount.setAccountType(3);
		shangjiaAccount.setAccountDate(dateFormat.format(new Date()));
		shangjiaAccount.setUserId(order.getUserId());
		shangjiaAccount.setObjectId(order.getOrderId());
		shangjiaAccountMapper.add(shangjiaAccount);
		
		//2.更新订单状态
		order.setPaymentTime(dateFormat.format(new Date()));
		orderMapper.payMent(order);
		
		//3.返还保证金（两种情况，一口价购买和竞价购买）
		ShangjiaAccount shangjiaAccount2=shangjiaAccountMapper.selectByAuctionInfoId(order.getAuctionInfoId());
		if(shangjiaAccount2!=null)
		{
			Account account2=new Account();
			account2.setUserId(shangjiaAccount2.getUserId());
			account2.setAccountType("4");
			account2.setAccountTrade(shangjiaAccount2.getAccountTrade());
			account2.setAccountDate(dateFormat.format(new Date()));
			account2.setAccountBalance(accountMapper.selectBalanceByUserId(shangjiaAccount2.getUserId())+shangjiaAccount2.getAccountTrade());
			accountMapper.add(account2);
			
			shangjiaAccount2.setAccountBalance(shangjiaAccountMapper.selectBalance() -shangjiaAccount2.getAccountTrade());
			shangjiaAccount2.setAccountTrade(shangjiaAccount2.getAccountTrade());
			shangjiaAccount2.setAccountType(2);
			shangjiaAccount2.setAccountDate(dateFormat.format(new Date()));
			//shangjiaAccount2.setUserId(auction.getUserId());
			shangjiaAccount2.setObjectId(order.getOrderId());
			shangjiaAccountMapper.add(shangjiaAccount2);
		}
		//4.
		
	}
	@Override
	public void withdrawOrder() {
		// TODO 自动生成的方法存根
		//1.订单失效
		//2.恢复库存
		orderMapper.withdrawOrder();
		orderMapper.confirm();//超时收货
	}
	@Override
	public List<Order> selectOrderByUser(int userId) {
		// TODO Auto-generated method stub
		return orderMapper.selectOrderByUser(userId);
	}
	@Override
	public List<Order> list2() {
		// TODO Auto-generated method stub
		return orderMapper.list2();
	}
	@Override
	public List<Order> list3(int orderState) {
		// TODO Auto-generated method stub
		return orderMapper.list3(orderState);
	}

	public List<Order> selectAll() {
		// TODO Auto-generated method stub
		return orderMapper.selectAll();
	}
	@Override
	public Order selectById(String orderId) {
		// TODO Auto-generated method stub
		return orderMapper.selectByOrderId(orderId);
	}
	@Override
	public List<Order> selectOrderAmount() {
		// TODO Auto-generated method stub
		return orderMapper.selectOrderAmount();
	}
	@Override
	public void updateOrderState(int orderState, String orderId,String time) {
		// TODO Auto-generated method stub
		orderMapper.updateOrderState(orderState, orderId,time);
	}
	@Override
	public void confirm(int orderState, String orderId, String time) {
		// TODO Auto-generated method stub
		orderMapper.confirmtime(orderState,orderId,time);
	}
}
